package com.d3works.apps.businessproject.service;

import com.d3works.apps.account.db.Invoice;
import com.d3works.apps.account.db.InvoiceLine;
import com.d3works.apps.account.service.AccountManagementAccountService;
import com.d3works.apps.account.service.AccountingSituationService;
import com.d3works.apps.account.service.analytic.AnalyticMoveLineGenerateRealService;
import com.d3works.apps.account.service.analytic.AnalyticMoveLineService;
import com.d3works.apps.account.service.app.AppAccountService;
import com.d3works.apps.account.service.move.MoveCreateService;
import com.d3works.apps.account.service.move.MoveValidateService;
import com.d3works.apps.account.service.moveline.MoveLineConsolidateService;
import com.d3works.apps.account.service.moveline.MoveLineCreateService;
import com.d3works.apps.account.service.payment.PaymentModeService;
import com.d3works.apps.base.db.repo.PeriodRepository;
import com.d3works.apps.base.service.BankDetailsService;
import com.d3works.apps.hr.db.ExpenseLine;
import com.d3works.apps.hr.db.repo.ExpenseRepository;
import com.d3works.apps.hr.service.KilometricService;
import com.d3works.apps.hr.service.config.AccountConfigHRService;
import com.d3works.apps.hr.service.config.HRConfigService;
import com.d3works.apps.hr.service.expense.ExpenseServiceImpl;
import com.d3works.apps.message.service.TemplateMessageService;
import com.d3works.exception.D3worksException;
import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.List;

public class ExpenseServiceProjectImpl extends ExpenseServiceImpl {

  @Inject
  public ExpenseServiceProjectImpl(
      MoveCreateService moveCreateService,
      MoveValidateService moveValidateService,
      ExpenseRepository expenseRepository,
      MoveLineCreateService moveLineCreateService,
      AccountManagementAccountService accountManagementService,
      AppAccountService appAccountService,
      AccountConfigHRService accountConfigService,
      AccountingSituationService accountingSituationService,
      AnalyticMoveLineService analyticMoveLineService,
      AnalyticMoveLineGenerateRealService analyticMoveLineGenerateRealService,
      HRConfigService hrConfigService,
      TemplateMessageService templateMessageService,
      PaymentModeService paymentModeService,
      PeriodRepository periodRepository,
      MoveLineConsolidateService moveLineConsolidateService,
      KilometricService kilometricService,
      BankDetailsService bankDetailsService) {
    super(
        moveCreateService,
        moveValidateService,
        expenseRepository,
        moveLineCreateService,
        accountManagementService,
        appAccountService,
        accountConfigService,
        accountingSituationService,
        analyticMoveLineService,
        analyticMoveLineGenerateRealService,
        hrConfigService,
        templateMessageService,
        paymentModeService,
        periodRepository,
        moveLineConsolidateService,
        kilometricService,
        bankDetailsService);
  }

  @Override
  public List<InvoiceLine> createInvoiceLines(
      Invoice invoice, List<ExpenseLine> expenseLineList, int priority) throws D3worksException {

    if (!appAccountService.isApp("business-project")) {
      return super.createInvoiceLines(invoice, expenseLineList, priority);
    }

    List<InvoiceLine> invoiceLineList = new ArrayList<InvoiceLine>();
    int count = 0;
    for (ExpenseLine expenseLine : expenseLineList) {

      invoiceLineList.addAll(this.createInvoiceLine(invoice, expenseLine, priority * 100 + count));
      count++;
      invoiceLineList.get(invoiceLineList.size() - 1).setProject(expenseLine.getProject());
    }

    return invoiceLineList;
  }
}
